package com.example.tellwin.utils;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;

/**
 * @author : LinZePeng
 * @date : 2020/11/11
 * @Describe ：
 **/
public class MediaUtils {

    public static int getMediaTime(Context context , Uri uri){
        try {
            MediaPlayer mediaPlayer = MediaPlayer.create(context ,uri);
            return mediaPlayer.getDuration();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static void playMedia(Context context , Uri uri){
        try {
            MediaPlayer mediaPlayer = MediaPlayer.create(context ,uri);
            mediaPlayer.start();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void playMediaUrl(Context context , String url){
        try {
            MediaPlayer mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(url);//设置播放来源
            mediaPlayer.prepareAsync();//异步准备
            mediaPlayer.start();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
